package com.orange.lockscreen.common;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;

import com.orange.lockscreen.common.Constants.LSWidget_Type;
import com.orange.lockscreen.common.Constants.Preference_Key;

public class LockScreenPreferences implements Preference_Key {

	public static final String	TAG								= "LockScreenPreferences";
	public static final String	LOCKSCREEN_SOUNDS_ENABLED		= "lockscreen_sounds_enabled";

	private static int			s_WallpaperIndex				= 0;
	private static boolean		sIsUnlockSoundEnabled			= false;
	private static boolean		sIsWallpaperFromGallery			= false;
	private static boolean		sIsEventViewEnabled				= true;
	private static boolean		sIsMusicViewEnabled				= true;
	private static boolean		sIsWallpaperFromHomeScreen		= true;
	private static boolean		sIsUnlockPromptEnabled			= true;
	private static boolean		sIsAlarmStatusEnabled			= true;
	private static boolean		sIsBatteryStatusEnabled			= true;
	private static boolean		sIsNotificationScreenEnabled	= true;
	private static boolean		sIsLockscreenDisabled			= false;
	private static boolean		sIsUnlockPatternEnabled			= false;

	public static void init(Context context) {
		sIsUnlockSoundEnabled = isNativeSoundEnabled(context);
		sIsUnlockPatternEnabled = isNativeUnlockPatternEnabled(context);

		try {
			Context otherContext = context.createPackageContext(
					Constants.PKG_LOCKSCREEN_SETTING,
					Context.CONTEXT_IGNORE_SECURITY);
			if (null != otherContext) {
				SharedPreferences sp = otherContext.getSharedPreferences(
						Constants.PREFERENCES_SETTINGS_NAME,
						Context.MODE_WORLD_WRITEABLE);

				s_WallpaperIndex = sp.getInt(KEY_WALLPAPER_INDEX, 0);
				sIsWallpaperFromGallery = sp.getBoolean(
						KEY_WALLPAPER_FROM_GALLERY, false);
				sIsWallpaperFromHomeScreen = sp.getBoolean(
						KEY_WALLPAPER_FROM_HOMESCREEN, true);
				sIsUnlockPromptEnabled = sp.getBoolean(KEY_UNLOCK_PROMPT, true);
				sIsAlarmStatusEnabled = sp.getBoolean(KEY_VIEW_TD_ALARM, true);
				sIsBatteryStatusEnabled = sp.getBoolean(KEY_VIEW_TD_BATTERY,
						true);

				sIsLockscreenDisabled = sp.getBoolean(KEY_DISABLE_LOCKSCREEN,
						false);

				sIsEventViewEnabled = sp
						.getBoolean(KEY_VIEW_EVENT_ENABLE, true);
				String musicSetting = sp.getString(KEY_VIEW_MUSIC_ENABLE, "0");

				sIsNotificationScreenEnabled = sp.getBoolean(
						KEY_VIEW_NOTIFICATION_ENABLE, true);
				try {
					int musicSet = Integer.parseInt(musicSetting);
					sIsMusicViewEnabled = musicSet < 0 ? false : true;
				} catch (Exception e) {
					LogHelper.e(TAG, "init. ", e);
				}
			}
		} catch (NameNotFoundException e) {
		}
	}

	private static boolean isNativeSoundEnabled(Context context) {
		int soundFlag = Settings.System.getInt(context.getContentResolver(),
				LOCKSCREEN_SOUNDS_ENABLED, 0);
		return soundFlag == 1 ? true : false;
	}

	private static boolean isNativeUnlockPatternEnabled(Context context) {
		int unlockPatternEnabled = 0;
		try {
			unlockPatternEnabled = Settings.System.getInt(
					context.getContentResolver(),
					Settings.System.LOCK_PATTERN_ENABLED);
		} catch (SettingNotFoundException e) {
		}
		return unlockPatternEnabled > 0 ? true : false;
	}

	public static boolean isLockscreenDisabled() {
		return sIsLockscreenDisabled;
	}

	public static int getIndexOfBG() {
		return s_WallpaperIndex;
	}

	public static boolean isWallpaperFromGallery() {
		return sIsWallpaperFromGallery;
	}

	public static boolean isWallpaperFromHomescreen() {
		return sIsWallpaperFromHomeScreen;
	}

	public static boolean isUnlockSoundEnabled() {
		return sIsUnlockSoundEnabled;
	}

	public static boolean isUnlockPatternEnabled() {
		return sIsUnlockPatternEnabled;
	}

	public static boolean isUnlockPromptEnabled() {
		return sIsUnlockPromptEnabled;
	}

	public static boolean isAlarmStatusEnabled() {
		return sIsAlarmStatusEnabled;
	}

	public static boolean isBatteryStatusEnabled() {
		return sIsBatteryStatusEnabled;
	}

	public static boolean isNotificationScreenEnabled() {
		return sIsNotificationScreenEnabled;
	}

	public static boolean getEnabledByViewType(int viewType) {
		boolean enabled = false;
		switch (viewType) {
		case LSWidget_Type.TYPE_EVENT:
			enabled = sIsEventViewEnabled;
			break;
		case LSWidget_Type.TYPE_MUSIC:
			enabled = sIsMusicViewEnabled;
			break;
		default:
			break;
		}
		return enabled;
	}
}
